package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblWpItemDiffSource;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpItemDiffSourceService;
import com.settlement.system.common.util.common.CommonAcsUtil;
import com.settlement.system.common.util.common.DictionaryUtil;
import com.settlement.system.common.util.common.FasUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 唯品会差异明细 前端控制器
 * @author Ximi
 * @since 2020-04-17
 */

@Slf4j
@RestController
@RequestMapping("/wp/itemDiffSource")
public class TblWpItemDiffSourceController extends BaseController<TblWpItemDiffSource> {

    @Resource
    private ITblWpItemDiffSourceService tblWpItemDiffSourceService;

    @Resource
    private DictionaryUtil dictionaryUtil;

    @Override
    public IBaseService init() {
        return tblWpItemDiffSourceService;
    }

    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        TblWpItemDiffSource foot = tblWpItemDiffSourceService.selectFoot(model);
        result.put("foot",foot);
        super.setFootModel(result, model);
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model,"shopNo","billNumberType","settleAccountPeriod",
                "brandNo","processStatus","categoryName","billNumber","calendar",
                "globalId","so","poNo","barcode","deliveryType");
        super.setParamsModel(model);
    }

    @Override
    protected ObjectDataResponse batchDelete(@RequestBody List<TblWpItemDiffSource> deleteList, HttpServletRequest request) throws ServiceException {
        try{
            String message = checkDeleteList(deleteList);
            if (StringUtils.isNotEmpty(message)) {
                return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), message);
            }
        } catch (Exception ex) {
            log.info("更新数据异常", ex);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
        return super.batchDelete(deleteList, request);
    }

    String checkDeleteList(List<TblWpItemDiffSource> deleteList) throws ServiceException {
        if(CollectionUtils.isEmpty(deleteList)){
            return "未选中任何数据";
        }

        // 允许删除的档期ID的列表： 10154124,10171986,10172658,10172659
        List<String> toBeDeletedScheduleIdList = dictionaryUtil.toBeDeletedScheduleIdList();

        for (TblWpItemDiffSource tblWpBillSaleDetail : deleteList) {
            if(!toBeDeletedScheduleIdList.contains(tblWpBillSaleDetail.getScheduleId())){
                return "非指定档期ID不允许删除";
            }
        }
        List<String> collect = deleteList.stream().map(TblWpItemDiffSource::getId).collect(Collectors.toList());
        List<List<String>> lists = CommonAcsUtil.splitList(collect, 10);

        String scheduleIdInQueryCondition = dictionaryUtil.toBeDeletedScheduleIdInQueryCondition();

        for(List<String> list : lists){
            int i = tblWpItemDiffSourceService.selectCountNotYk(list, scheduleIdInQueryCondition);
            if(i>0){
                return "非指定档期ID不允许删除.";
            }
        }

        return null;
    }

}

